package com.dmooo.ymx.mall;

import android.Manifest;
import android.app.Activity;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Outline;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewOutlineProvider;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.dmooo.ymx.R;
import com.dmooo.ymx.activity.ElemeActivity;
import com.dmooo.ymx.activity.JdActivity;
import com.dmooo.ymx.activity.JiayoulistActivity;
import com.dmooo.ymx.activity.NewsActivity;
import com.dmooo.ymx.activity.PromotionDetailsActivity;
import com.dmooo.ymx.activity.TianMaoShuangShiyiActivity;
import com.dmooo.ymx.activity.WebViewActivity;
import com.dmooo.ymx.activity.WebViewActivity2;
import com.dmooo.ymx.activity.XuanpinkActivity;
import com.dmooo.ymx.activity.ZeroBuyActivity;
import com.dmooo.ymx.activity.ZhouZhouLeActivity;
import com.dmooo.ymx.base.BaseLazyFragment;
import com.dmooo.ymx.bean.BannerBean;
import com.dmooo.ymx.bean.MessageCenterBean;
import com.dmooo.ymx.bean.MessageEvent;
import com.dmooo.ymx.bean.Response;
import com.dmooo.ymx.common.SPUtils;
import com.dmooo.ymx.common.T;
import com.dmooo.ymx.config.Constants;
import com.dmooo.ymx.fragments.HomeFirstFragment;
import com.dmooo.ymx.fragments.MyFragment;
import com.dmooo.ymx.https.HttpUtils;
import com.dmooo.ymx.https.onOKJsonHttpResponseHandler;
import com.dmooo.ymx.malladapter.MallHomeFirstAdapter;
import com.dmooo.ymx.mallbean.ShopMallGoodsBean;
import com.dmooo.ymx.merchantactivity.MerchanthomeActivity;
import com.dmooo.ymx.merchantactivity.MerchantlistActivity;
import com.dmooo.ymx.merchantadapter.MerchanthomeIconAdapter;
import com.dmooo.ymx.merchantbean.MerchantGroupbean;
import com.dmooo.ymx.utils.BannerImageView;
import com.dmooo.ymx.utils.CornerTransform;
import com.dmooo.ymx.utils.MyRecyclerView2;
import com.dmooo.ymx.utils.RoundImageView;
import com.dmooo.ymx.utils.ScrollBanner;
import com.dmooo.ymx.utils.StickHeaderDecoration;
import com.dmooo.ymx.widget.CircleImageView;
import com.gcssloop.widget.PagerGridLayoutManager;
import com.gcssloop.widget.PagerGridSnapHelper;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;
import com.mylhyl.acp.Acp;
import com.mylhyl.acp.AcpListener;
import com.mylhyl.acp.AcpOptions;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.loader.ImageLoader;
import com.zhouwei.mzbanner.MZBannerView;
import com.zhouwei.mzbanner.holder.MZHolderCreator;
import com.zhouwei.mzbanner.holder.MZViewHolder;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.RequiresApi;
import butterknife.BindView;
import butterknife.ButterKnife;
import cz.msebera.android.httpclient.Header;

public class MallFragmentFirst extends BaseLazyFragment implements View.OnClickListener {

    private RecyclerView  merchantrecy;
    private MyRecyclerView2 typerecy;
    private TextView tv_selectaddress;

    ImageView right_icon;

    private MZBannerView banner;
    private EditText et_seek;
    private MerchanthomeIconAdapter merchanthomeIconAdapter;
    private SmartRefreshLayout smartRefreshLayout;
    private RadioGroup page_rg;
    public boolean isdw = true;
    private int page = 1;
    private LinearLayout headLayout;
    View view;
    private MallHomeFirstAdapter merchantlistAdapter;
    private String orderby = "synthesize";
    int scrollY=0;
    private ScrollBanner scrollBanner;
    ImageView img1,img2,img3,img4,img5,img6,tj_img;
    private LinearLayout home_layout;
    private MZBannerView banner2;
    private List<BannerBean> images3 = new ArrayList<>();
    private List<MessageCenterBean.MessageCenterChildBean> images2 = new ArrayList<>();
    List<String> strings;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.activity_merchantmain, container, false);
        ButterKnife.bind(this, view);
        view.findViewById(R.id.ll_top).setVisibility(View.GONE);
        right_icon=view.findViewById(R.id.right_icon);
        initUI();
        initData();
        return view;
    }

    protected void initUI() {
        TextView t = view.findViewById(R.id.txt_two);
        t.setText("销量");
        t = view.findViewById(R.id.txt_three);
        t.setText("价格");
        t = view.findViewById(R.id.txt_four);
        t.setText("最新上架");
        headLayout = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.layout_head_merchant, null);
        headLayout.findViewById(R.id.sc_mall).setVisibility(View.GONE);//首页顶部的图片
//        sortHeader=(LinearLayout) LayoutInflater.from(this).inflate(R.layout.sort_header,null);
//        headLayout.findViewById(R.id.ll_tool).setVisibility(View.VISIBLE);
        typerecy = headLayout.findViewById(R.id.merchanthome_typerecy);
        scrollBanner = headLayout.findViewById(R.id.main_scroll);
        // 1.水平分页布局管理器
        PagerGridLayoutManager layoutManager = new PagerGridLayoutManager(
                2, 5, PagerGridLayoutManager.HORIZONTAL);
        layoutManager.setPageListener(new PagerGridLayoutManager.PageListener() {
            @Override
            public void onPageSizeChanged(int pageSize) {
            }

            @Override
            public void onPageSelect(int pageIndex) {
                page_rg.check(pageIndex);
            }
        });
        page_rg = headLayout.findViewById(R.id.page_rg);
        layoutManager.setChangeSelectInScrolling(true);
        typerecy.setLayoutManager(layoutManager);
// 2.设置滚动辅助工具
        PagerGridSnapHelper pageSnapHelper = new PagerGridSnapHelper();
        pageSnapHelper.attachToRecyclerView(typerecy);
        merchanthomeIconAdapter = new MerchanthomeIconAdapter(context, R.layout.service_home_grid_item, iconlist);
        typerecy.setAdapter(merchanthomeIconAdapter);
        merchantrecy = view.findViewById(R.id.merchanthome_merchantrecy);
        banner = headLayout.findViewById(R.id.merchanthome_banner);

        banner.setVisibility(View.VISIBLE);//首页banner图
        smartRefreshLayout = view.findViewById(R.id.refresh_layout);
        et_seek = view.findViewById(R.id.merchanthome_etseek);
        tv_selectaddress = view.findViewById(R.id.merchanthome_selectaddress);

        banner2 = headLayout.findViewById(R.id.home_ad);
        headLayout.findViewById(R.id.ll_home_six).setOnClickListener(this);
        headLayout.findViewById(R.id.ll_home_two).setOnClickListener(this);
        img1=headLayout.findViewById(R.id.ll_home_six);
        img2=headLayout.findViewById(R.id.ll_home_two);
        tj_img=headLayout.findViewById(R.id.tj_img);
        home_layout=headLayout.findViewById(R.id.home_layout);
        tj_img.setVisibility(View.VISIBLE);
        img1.setVisibility(View.VISIBLE);
        img2.setVisibility(View.VISIBLE);
        home_layout.setVisibility(View.VISIBLE);
        tv_selectaddress.setOnClickListener(this);
        view.findViewById(R.id.txt_search).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                smartRefreshLayout.autoRefresh();
            }
        });
//        smartRefreshLayout.setEnableRefresh(false);
        smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshlayout) {
                page = 1;
                getmerchantlist();
                //refreshlayout.finishRefresh(2000/*,false*/);//传入false表示刷新失败
            }
        });
        smartRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                page++;
                getmerchantlist();
                //refreshlayout.finishLoadMore(2000/*,false*/);//传入false表示加载失败
            }
        });
        getmerchantlist();
        merchantrecy.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                scrollY+=dy;
                if(scrollY>=headLayout.getMeasuredHeight()){
                    view.findViewById(R.id.ll_sort).setVisibility(View.GONE);
                    right_icon.setVisibility(View.VISIBLE);
                }else{
                    view.findViewById(R.id.ll_sort).setVisibility(View.GONE);
                    right_icon.setVisibility(View.GONE);
                }
            }

        });
        scrollBanner.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (images2.size() > 0) {
                    Intent intent = new Intent(context, NewsActivity.class);
                    intent.putExtra("title", images2.get(scrollBanner.getCurrentPosition() == images2.size() - 1 ? 0 : scrollBanner.getCurrentPosition() + 1).getTitle());
                    intent.putExtra("article_id", images2.get(scrollBanner.getCurrentPosition() == images2.size() - 1 ? 0 : scrollBanner.getCurrentPosition() + 1).getArticle_id());
                    startActivity(intent);
                }
            }
        });


        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                getHomeADimg();
                getAD();
                getBannerMsg();
                getBannerMsg1();
            }
        }, 100);

        banner.setBannerPageClickListener(new MZBannerView.BannerPageClickListener() {
            @Override
            public void onPageClick(View view, int position) {
                Log.d("ddddd",images3.get(position).getType());
                if ("1".equals(images3.get(position).getType())) {
                    if ("".equals(images3.get(position).getHref())) {
                        return;
                    }
                    Intent intent = new Intent(context, WebViewActivity.class);
                    intent.putExtra("title", images3.get(position).getTitle());
                    intent.putExtra("url", images3.get(position).getHref());
                    startActivity(intent);
                } else if ("2".equals(images3.get(position).getType())) {
                    Intent intent = context.getPackageManager().getLaunchIntentForPackage("com.taobao.taobao");
                    if (intent != null) {
                        //获取剪贴板管理器：
                        ClipboardManager cm = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
                        // 创建普通字符型ClipData
                        ClipData mClipData = ClipData.newPlainText("Label", images3.get(position).getType_value());
                        // 将ClipData内容放到系统剪贴板里。
                        cm.setPrimaryClip(mClipData);
                        startActivity(intent);
                        return;
                    } else {
                        T.showShort(context, "未安装淘宝客户端");
                    }
                } else if ("3".equals(images3.get(position).getType())) {
                    Uri uri = Uri.parse(images3.get(position).getType());
                    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                    startActivity(intent);
                } else if ("4".equals(images3.get(position).getType())) {
                    Uri uri = Uri.parse(images3.get(position).getType());
                    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                    startActivity(intent);
                    return;
                } else if ("6".equals(images3.get(position).getType())) {
                    Intent intent = new Intent(context, WebViewActivity2.class);
                    intent.putExtra("title", "年货节");
                    intent.putExtra("url", "");
                    startActivity(intent);
                    return;
                } else if ("7".equals(images3.get(position).getType()) || "8".equals(images3.get(position).getType())) {
//                    hongbao();
                    return;
                }else if ("16".equals(images3.get(position).getType())) {
                    //进洞
                    openActivity(ElemeActivity.class);
                    return;
                }  else if ("9".equals(images3.get(position).getType())) {
                    Bundle bundle = new Bundle();
                    bundle.putString("num_iid", images3.get(position).getType_value());
                    openActivity(PromotionDetailsActivity.class, bundle);
                    return;
                } else if ("10".equals(images3.get(position).getType())) {
                    Intent intent = new Intent(context, WebViewActivity.class);
                    intent.putExtra("title", "拉新活动");
                    intent.putExtra("url", Constants.APP_IP + "/wap.php/Rookie/index/uid/" + SPUtils.getStringData(context, Constants.UID, ""));
                    startActivity(intent);
                    return;
                } else if ("11".equals(images3.get(position).getType())) {
                    openActivity(ZeroBuyActivity.class);
                    return;
                } else if ("12".equals(images3.get(position).getType())) {
                    openActivity(XuanpinkActivity.class);
                    return;
                } else if ("17".equals(images3.get(position).getType())) {
                    Bundle bundle=new Bundle();
                    openActivity(ZhouZhouLeActivity.class);
//                    zhouzhoule();
                    return;
                } else if ("13".equals(images3.get(position).getType())) {
                    //进洞
                    openActivity(JdActivity.class);
                    return;
                } else if ("14".equals(images3.get(position).getType())) {
                    Bundle bundle = new Bundle();
                    bundle.putString("title", images3.get(position).getTitle());
                    bundle.putString("material_id", images3.get(position).getType_value());
                    openActivity(TianMaoShuangShiyiActivity.class, bundle);
                    return;
                } else if ("15".equals(images3.get(position).getType())) {
                    Acp.getInstance(context).request(new AcpOptions.Builder()
                            .setPermissions(Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION)
                            .build(), new AcpListener() {
                        @Override
                        public void onGranted() {
                            openActivity(JiayoulistActivity.class);
                        }

                        @Override
                        public void onDenied(List<String> permissions) {
                            showToast("加油站需要您的定位权限");
                        }
                    });
                    return;
                }
            }
        });
    }

    private void getHomeADimg() {
        RequestParams requestParams = new RequestParams();
        requestParams.put("cat_id", 3);
        HttpUtils.post(Constants.GET_BANNER, requestParams, new onOKJsonHttpResponseHandler<BannerBean>(new TypeToken<Response<BannerBean>>() {
        }) {
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
//                showToast(throwable.getMessage());
            }

            @Override
            public void onSuccess(int statusCode, Response<BannerBean> datas) {
                if (datas.isSuccess()) {
                    images3.clear();
                    images3.addAll(datas.getData().getList());
                    List<String> strings = new ArrayList<>();
                    for (int i = 0; i < images3.size(); i++) {
                        strings.add(Constants.APP_IP + images3.get(i).getImg());
                    }
                    if (images3.size() <= 0) {
                        banner2.setVisibility(View.GONE);
                    } else {
                        banner2.setVisibility(View.GONE);
                        banner2.setPages(strings, new MZHolderCreator<BannerViewHolder>() {
                            @Override
                            public BannerViewHolder createViewHolder() {
                                return new BannerViewHolder();
                            }
                        });
                        banner2.start();
                    }
                } else {
                    showToast(datas.getMsg());
                }
            }
        });
    }

    class BannerViewHolder implements MZViewHolder<String> {
        private ImageView mImageView;

        @Override
        public View createView(Context context) {
            View view = LayoutInflater.from(context).inflate(R.layout.img_t, null);
            mImageView = view.findViewById(R.id.img);
            return view;
        }

        @Override
        public void onBind(Context context, int position, final String data) {
            Glide.with(context).load(data).into(mImageView);
        }
    }

    private void getAD() {
        RequestParams requestParams = new RequestParams();
        requestParams.put("cat_id", 4);
        HttpUtils.post(Constants.MESSAGE_ARTICLE_GETARTICLELIST_URL, requestParams, new onOKJsonHttpResponseHandler<MessageCenterBean>(new TypeToken<Response<MessageCenterBean>>() {
        }) {
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
//                showToast(throwable.getMessage());
            }

            @Override
            public void onSuccess(int statusCode, Response<MessageCenterBean> datas) {
                if (datas.isSuccess()) {
                    if (datas.getData().getList().size() <= 0) {
                        return;
                    }
                    images2.clear();
                    images2.addAll(datas.getData().getList());
                    List<String> strings = new ArrayList<>();
                    for (int i = 0; i < images2.size(); i++) {
                        strings.add(images2.get(i).getTitle());
                    }
                    if (images2.size() > 0)
                        strings.add(images2.get(0).getTitle());
                    scrollBanner.setList(strings);
                    scrollBanner.startScroll();
                } else {
                    showToast(datas.getMsg());
                }
            }
        });
    }

    protected void initData() {
//        merchanthomeIconAdapter = new MerchanthomeIconAdapter(this,R.layout.service_home_grid_item,iconlist);
//        typerecy.setLayoutManager(new GridLayoutManager(this,5));
//        typerecy.setAdapter(merchanthomeIconAdapter);
        merchanthomeIconAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                Bundle bundle=new Bundle();
                bundle.putString("name",iconlist.get(position).cat_name);
                bundle.putString("pid",iconlist.get(position).cat_id);
                openActivity(ShopMallActivty.class,bundle);
            }
            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });
//        getauthlist();
        getBanner();
        geticon();
        merchantlistAdapter = new MallHomeFirstAdapter(merchantlistbeans);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(context, 2);
        merchantrecy.setLayoutManager(gridLayoutManager);//
        merchantlistAdapter.addHeaderView(headLayout);
//        merchantlistAdapter.addHeaderView(sortHeader);
        merchantrecy.setAdapter(merchantlistAdapter);
//        gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
//            @Override
//            public int getSpanSize(int position) {
//                if (position == 0) {
//                    return 2;
//                }
//                return position == 1 ? 2 : 1;
//            }
//        });
//        merchantrecy.addItemDecoration(new StickHeaderDecoration(merchantrecy));
        merchantlistAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (view.getId() == R.id.txt_one) {
                    page = 1;
                    orderby = "synthesize";
                    getmerchantlist();
                } else if (view.getId() == R.id.txt_two) {
                    page = 1;
                    orderby = "sales";
                    getmerchantlist();

                } else if (view.getId() == R.id.txt_three) {
                    page = 1;
                    if ("price1".equals(orderby)) {
                        orderby = "price2";
                    } else {
                        orderby = "price1";
                    }
                    getmerchantlist();
                } else if (view.getId() == R.id.txt_four) {
                    page = 1;
                    orderby = "new";
                    getmerchantlist();
                }
            }
        });
        view.findViewById(R.id.txt_two).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                page = 1;
                orderby = "sales";
                getmerchantlist();
            }
        });
        view.findViewById(R.id.txt_one).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                page = 1;
                orderby = "synthesize";
                getmerchantlist();
            }
        });
        view.findViewById(R.id.txt_four).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                page = 1;
                orderby = "new";
                getmerchantlist();
            }
        });
        view.findViewById(R.id.txt_three).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                page = 1;
                if ("price1".equals(orderby)) {
                    orderby = "price2";
                } else {
                    orderby = "price1";
                }
                getmerchantlist();
            }
        });

        view.findViewById(R.id.right_icon).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                merchantrecy.post(new Runnable() {
                    @Override
                    public void run() {
                        merchantrecy.smoothScrollToPosition(0);
                    }
                });
            }
        });
    }


    @RequiresApi(api = Build.VERSION_CODES.M)
    public void setbanner() {
        banner.setOutlineProvider(new ViewOutlineProvider() {
            @Override
            public void getOutline(View view, Outline outline) {
                outline.setRoundRect(0, 0, view.getWidth(), view.getHeight(), 30);
            }
        });
        banner.setClipToOutline(true);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            if (images.get(0).getColor() == null||"".equals(images.get(0).getColor())) {
                images.get(0).setColor("");
            }
//                    homeBg.getDrawable().setTint(Color.parseColor(images.get(position).getColor()));
            SPUtils.saveStringData(context, "color", images.get(0).getColor());
            try{
                smartRefreshLayout.setPrimaryColors(Color.parseColor(images.get(0).getColor()), Color.WHITE);
            }catch (Exception e){
                e.printStackTrace();
            }
            MessageEvent event = new MessageEvent(images.get(0).getColor());
            EventBus.getDefault().post(event);
        }
        banner.setBannerPageClickListener(new MZBannerView.BannerPageClickListener() {
            @Override
            public void onPageClick(View view, int position) {
//                showToast(""+position);

            }
        });
        banner.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
            @Override
            public void onLayoutChange(View view, int i, int i1, int i2, int i3, int i4, int i5, int i6, int i7) {

            }
        });
        banner.addPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    if (images.get(position).getColor() == null||"".equals(images.get(position).getColor())) {
                        images.get(position).setColor("");
                    }
                    SPUtils.saveStringData(context, "color", images.get(position).getColor());
                    try{
                        smartRefreshLayout.setPrimaryColors(Color.parseColor(images.get(position).getColor()), Color.WHITE);
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                    MessageEvent event = new MessageEvent(images.get(position).getColor());
                    EventBus.getDefault().post(event);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
//        banner.setDelayedTime(5000);
        banner.setIndicatorRes(R.color.colorAccent,R.color.colorPrimary);
        banner.setPages(strings, new MZHolderCreator<BannerViewHolder>() {
            @Override
            public BannerViewHolder createViewHolder() {
                return new BannerViewHolder();
            }
        });
        banner.setIndicatorVisible(true);
    }

    private List<BannerBean> images = new ArrayList<>();

    private void getBanner() {
        RequestParams requestParams = new RequestParams();
        requestParams.put("cat_id", 11);
        HttpUtils.post(Constants.GET_BANNER, requestParams, new onOKJsonHttpResponseHandler<BannerBean>(new TypeToken<Response<BannerBean>>() {
        }) {
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
//                showToast(throwable.getMessage());
            }

            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onSuccess(int statusCode, Response<BannerBean> datas) {
                if (datas.isSuccess()) {
                    images.clear();
                    images.addAll(datas.getData().getList());
                    strings = new ArrayList<>();
                    for (int i = 0; i < images.size(); i++) {
                        strings.add(Constants.APP_IP + images.get(i).getImg());
                    }
//                    banner.update(strings);
                    setbanner();
                    banner.start();
                    banner.setDelayedTime(3000);
                } else {
                    showToast(datas.getMsg());
                }
            }
        });
    }

    private List<MerchantGroupbean> iconlist = new ArrayList<>();

    private void geticon() {
        RequestParams requestParams = new RequestParams();
        HttpUtils.post(Constants.SLEF_MALL_CAT, requestParams, new TextHttpResponseHandler() {
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
            }


            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {
                try {
                    JSONObject object = new JSONObject(responseString);
                    if ("0".equals(object.getString("code"))) {
                        JSONArray array = object.getJSONObject("data").getJSONArray("list");
                        for (int i = 0; i < array.length(); i++) {
                            iconlist.add(new Gson().fromJson(array.getJSONObject(i).toString(), MerchantGroupbean.class));
                        }
                        int count = (iconlist.size() - 1) / 10 + 1;
                        for (int i = 0; i < count; i++) {
                            RadioButton rb = (RadioButton) LayoutInflater.from(context).inflate(R.layout.item_page_group, null);
                            rb.setId(i);
                            if (i == 0) {
                                rb.setChecked(true);
                            }
                            page_rg.addView(rb);
                        }

                        merchanthomeIconAdapter.notifyDataSetChanged();
                    } else {
                        showToast(object.getString("msg"));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }
    public List<ShopMallGoodsBean> merchantlistbeans = new ArrayList<>();
    private void getmerchantlist() {
        RequestParams requestParams = new RequestParams();
        requestParams.put("p", page);
        requestParams.put("cat_id", "");
        requestParams.put("per", 10);
        requestParams.put("orderby", orderby);
        HttpUtils.post(Constants.SLEF_MALL_GOODS, requestParams, new onOKJsonHttpResponseHandler<ShopMallGoodsBean>(new TypeToken<Response<ShopMallGoodsBean>>() {
        }) {
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                showToast(responseString);
            }

            @Override
            public void onSuccess(int statusCode, Response<ShopMallGoodsBean> datas) {
                if (datas.isSuccess()) {
                    List<ShopMallGoodsBean> list = datas.getData().list;
                    if (page == 1) {
                        merchantlistbeans.clear();
//                        merchantlistbeans.add(new ShopMallGoodsBean());
                    }
                    merchantlistbeans.addAll(list);
                } else {
                    showToast(datas.getMsg());
                }
                merchantlistAdapter.notifyDataSetChanged();
                if (smartRefreshLayout != null) {
                    if (page == 1) {
                        smartRefreshLayout.finishRefresh();
                    } else {
                        smartRefreshLayout.finishLoadMore();
                    }
                }
            }
        });
    }

    //    //获取服务
//    public List<Authbean> authbeanList = new ArrayList<>();
//    private void getauthlist() {
//        RequestParams requestParams = new RequestParams();
//        HttpUtils.post(Constants.getMerchantAuth, requestParams, new TextHttpResponseHandler() {
//            @Override
//            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
//
//            }
//
//
//            @Override
//            public void onSuccess(int statusCode, Header[] headers, String responseString) {
//                Log.d("dsfasd", responseString);
//                try {
//                    JSONObject object = new JSONObject(responseString);
//                    if ("0".equals(object.getString("code"))) {
//                        JSONArray array = object.getJSONObject("data").getJSONArray("list");
//                        for (int i = 0; i < array.length(); i++) {
//                            authbeanList.add(new Gson().fromJson(array.getJSONObject(i).toString(), Authbean.class));
//                        }
//                    } else {
//                        showToast(object.getString("msg"));
//                    }
//                } catch (JSONException e) {
//                    e.printStackTrace();
//                }
//            }
//        });
//    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.merchanthome_selectaddress:
                break;
            case R.id.ll_home_two://本地店铺
                Acp.getInstance(context).request(new AcpOptions.Builder()
                        .setPermissions(Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION)
                        .build(), new AcpListener() {
                    @Override
                    public void onGranted() {
                        openActivity(MerchanthomeActivity.class);
                    }

                    @Override
                    public void onDenied(List<String> permissions) {
                        showToast("需要您的定位权限");
                    }
                });
                break;
            case R.id.ll_home_six:
                //官方旗舰店dd
//                ddddd
                if("".equals(SPUtils.getStringData(context, "token", ""))){
                    showToast("请先登录");
                    return;
                }
                openActivity( MyShopMallActivity.class);
//                intent = new Intent(getActivity(), BaoYouActivity.class);
//                intent.putExtra("type", "1");
//                startActivity(intent);
                break;
        }
    }

    /**
     *获取个人中心banner广告信息  签到有礼
     */
    private void getBannerMsg() {
//        if("".equals(SPUtils.getStringData(context, "token", ""))){
//            return;
//        }
        RequestParams params = new RequestParams();
        params.put("id",107);
        HttpUtils.post(Constants.GET_BANNER_DETAIL, params, new TextHttpResponseHandler() {
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                // 输出错误信息
            }

            @Override
            public void onFinish() {
                // 隐藏进度条
                super.onFinish();
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {
                try {
                    JSONObject jsonObject = new JSONObject(responseString);
                    //返回码
                    int code = jsonObject.optInt("code");
                    //返回码说明
                    String msg = jsonObject.optString("msg");
                    if (0 == code) {
                        Glide.with(context).load(Constants.APP_IP+jsonObject.getJSONObject("data").getJSONObject("bannerMsg").getString("img")).into(img1);
                    } else {
                        showToast(msg);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onStart() {
                super.onStart();
            }
        });
    }

    /**
     *获取个人中心banner广告信息  升级礼包
     */
    private void getBannerMsg1() {
//        if("".equals(SPUtils.getStringData(context, "token", ""))){
//            return;
//        }
        RequestParams params = new RequestParams();
        params.put("id",108);
        HttpUtils.post(Constants.GET_BANNER_DETAIL, params, new TextHttpResponseHandler() {
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                // 输出错误信息
            }

            @Override
            public void onFinish() {
                // 隐藏进度条
                super.onFinish();
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {
                try {
                    JSONObject jsonObject = new JSONObject(responseString);
                    //返回码
                    int code = jsonObject.optInt("code");
                    //返回码说明
                    String msg = jsonObject.optString("msg");
                    if (0 == code) {
                        Glide.with(context).load(Constants.APP_IP+jsonObject.getJSONObject("data").getJSONObject("bannerMsg").getString("img")).into(img2);
                    } else {
                        showToast(msg);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onStart() {
                super.onStart();
            }
        });
    }

    @Override
    protected void lazyload() {

    }
}
